/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.FileInfo;
import com.hboxs.asl.FileInfo.FileType;
import com.hboxs.asl.FileInfo.OrderType;
import com.hboxs.asl.Message;
import com.hboxs.asl.entity.ProductImage;
import com.hboxs.asl.service.FileService;
import com.hboxs.asl.util.JsonUtils;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller - 文件处理
 */
@Controller("adminFileController")
@RequestMapping("/asl-admin/file")
public class FileController extends BaseController {

    @Resource(name = "fileServiceImpl")
    private FileService fileService;

    /**
     * 上传
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST, produces = "text/html; charset=UTF-8")
    public void upload(FileType fileType, MultipartFile file, HttpServletResponse response, HttpSession session) {
        Map<String, Object> data = new HashMap<String, Object>();
        if (!fileService.isValid(fileType, file)) {
            data.put("message", Message.warn("admin.upload.invalid"));
        } else {
            Enum imageType = (Enum) session.getAttribute(IMAGE_TYPE);
            if (imageType == null) {
                imageType = ProductImage.imageType.defaultImage;
            }
            String url = fileService.upload(fileType, file, false, imageType.toString());
            if (url == null) {
                data.put("message", Message.warn("admin.upload.error"));
            } else {
                data.put("message", SUCCESS_MESSAGE);
                data.put("url", url);
            }
        }
        try {
            response.setContentType("text/html; charset=UTF-8");
            JsonUtils.writeValue(response.getWriter(), data);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 浏览
     */
    @RequestMapping(value = "/browser", method = RequestMethod.GET)
    public
    @ResponseBody
    List<FileInfo> browser(String path, FileType fileType, OrderType orderType) {
        return fileService.browser(path, fileType, orderType);
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(String path , ModelMap model) {
        FileType fileType = FileType.image;
        OrderType orderType = OrderType.name;
        List<FileInfo> fileInfos = fileService.browser(path, fileType, orderType);
        model.addAttribute("fileInfos" , fileInfos);
        model.addAttribute("path" , path);
        return "/admin/file/list";
    }


    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message del(String[] ids , ModelMap model) {
        FileType fileType = FileType.image;
        OrderType orderType = OrderType.name;
        for (int i = 0; i < ids.length; i++) {
            fileService.del(fileType, ids[i]);
        }
        return SUCCESS_MESSAGE;
    }


}